package com.hzw.saas.service.hpc.listener.handler;

import cn.hutool.core.util.StrUtil;
import com.hzw.saas.api.hpc.IHpcAccountProviderService;
import com.hzw.saas.api.hpc.bo.HpcAccountBo;
import com.hzw.saas.api.hpc.bo.HpcAccountProviderBo;
import com.hzw.saas.service.hpc.event.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * 抽象的HPC用户第三方处理器
 *
 * @author sonam
 * @sine 2021/9/8 3:34 下午
 */
@Slf4j
@RequiredArgsConstructor
public abstract class AbstractHpcAccountHandler {

    private final IHpcAccountProviderService providerService;

    public void execute(HpcAccountBo hpcAccountBo, HpcAccountEvent event) {
        String providerId = hpcAccountBo.getProviderId();
        if (StrUtil.isBlank(providerId)) {
            return;
        }
        HpcAccountProviderBo providerBo = providerService.getById(providerId);
        if(Objects.isNull(providerBo)) {
            return;
        }
        log.info("供应商 {} == {}", providerBo.getProviderCode(), getProviderCode());
        if (Objects.equals(providerBo.getProviderCode(), getProviderCode())) {
            if (event instanceof HpcAccountAddEvent) {
                doHpcAccountAddHandle(providerBo, hpcAccountBo);
            } else if (event instanceof HpcAccountDeleteEvent) {
                doHpcAccountDeleteHandle(providerBo, hpcAccountBo);
            } else if (event instanceof HpcAccountAllotEvent) {
                doHpcAccountAllotHandle(providerBo, hpcAccountBo);
            } else if (event instanceof HpcAccountUnAllotEvent) {
                doHpcAccountUnAllotHandle(providerBo, hpcAccountBo);
            } else {
                log.warn("不能处理的事件类型>>>{}<<<", event);
            }
        }
    }

    /**
     * HPC账号添加处理
     *
     * @param hpcAccountBo
     */
    protected abstract void doHpcAccountAddHandle(HpcAccountProviderBo providerBo, HpcAccountBo hpcAccountBo);

    /**
     * HPC账号删除处理
     *
     * @param hpcAccountBo
     */
    protected abstract void doHpcAccountDeleteHandle(HpcAccountProviderBo providerBo, HpcAccountBo hpcAccountBo);

    /**
     * 分配Slurm用户
     * @param providerBo
     * @param hpcAccountBo
     */
    protected abstract void doHpcAccountAllotHandle(HpcAccountProviderBo providerBo, HpcAccountBo hpcAccountBo);

    /**
     * 解除Slurm用户绑定
     * @param providerBo
     * @param hpcAccountBo
     */
    protected abstract void doHpcAccountUnAllotHandle(HpcAccountProviderBo providerBo, HpcAccountBo hpcAccountBo);

    /**
     * 获取供应商名称
     *
     * @return
     */
    protected abstract String getProviderCode();

}
